package com.sunday.ble.orlant.utils;

import android.app.Dialog;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.sunday.ble.orlant.R;
import com.sunday.ble.orlant.adapter.BlueToothAdapter;
import com.sunday.common.widgets.UIAlertView;

import java.util.ArrayList;
import java.util.List;


/**
 * 仿IOS的弹出框
 *
 * @来自网上
 */
public class UpdateNameDialogView extends Dialog {

    private Context context;
    private String title;
    private ClickListenerInterface clickListenerInterface;

    public UpdateNameDialogView(Context context, String title) {
        super(context, R.style.UIAlertViewStyle);
        this.context = context;
        this.title = title;
    }

    public interface ClickListenerInterface {
        void doLeft();

        void doRight(String name);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        inite();
    }
    EditText name;
    public void inite() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.rename_dialog_view, null);
        setContentView(view);
        TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        TextView tvLeft = (TextView) view.findViewById(R.id.tvBtnLeft);
        TextView tvRight = (TextView) view.findViewById(R.id.tvBtnRight);
        name = (EditText) view.findViewById(R.id.etName);
        if ("".equals(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setText(title);
        }
        tvLeft.setText("取消");
        tvRight.setText("确定");

        tvLeft.setOnClickListener(new clickListener());
        tvRight.setOnClickListener(new clickListener());
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics();

        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }
    private class clickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {

            String  tag = (String)v.getTag();
            switch (tag) {
                case "tvBtnLeft":
                    clickListenerInterface.doLeft();
                    break;
                case "tvBtnRight":
                    clickListenerInterface.doRight(name.getText().toString().trim());
                    break;
                default:
                    break;
            }
        }
    }
}
